package org.mmtk.plan.scale.colors;

import org.mmtk.plan.scale.utils.ArayHelper;
import org.vmmagic.pragma.Uninterruptible;
import org.vmmagic.unboxed.ObjectReference;
import org.vmmagic.unboxed.ObjectReferenceArray;

@Uninterruptible
public class ColorsArray {
	private int size;
	private int next;
	private ObjectReferenceArray colors;
	
	public ColorsArray(int size) {	
		this.size = size;
		this.colors = ObjectReferenceArray.create(size);	
		init();
	}
	
	public int getCurrentSize() {
		return this.next;
	}
	
	public void init() {
		this.next = 0;
		ArayHelper.zeroArray(this.colors);
	}
	
	public void addColor(ObjectReference color) {
		if(!hasColor(color) && next < size) {
			this.colors.set(next,color);
			next++;
		}
	}
	
	public boolean hasColor(ObjectReference color) {
		boolean result = false;
		
		for(int i = 0; i < next; i++) {
			if(colors.get(i) == color) {
				result = true;
				break;
			}
		}
		
		return result;
	}
	
	public void dump() {
		ArayHelper.dumpArray(this.colors, this.next);
	}
}
